package com.jds.test.consoleim.client;

import com.jds.test.consoleim.BaseBean;
import com.jds.test.consoleim.Client4;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by sunyuming on 18/8/17.
 */
public class ThreadConsole implements Runnable {

    public void run() {
        try {

            BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
            String msg = null;
            while (true) {

                msg = re.readLine();
                decodeMsg(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析用户输入
     * @param msg
     * @return
     */
    void decodeMsg(String msg) {
        if(0 == msg.indexOf("-t ")) {
            // 设置传输对象
            String [] item = msg.split("-t ");
            String to = item[1];
            Client4.clientHandler4.to = to;

        } else if(0 == msg.indexOf("-l ")) {
            // 登陆 或 重设昵称
            String [] item = msg.split("-l ");
            String user = item[1];
            Client4.clientHandler4.type = 0;
            Client4.clientHandler4.send(user);

        }else if("_q".equals(msg)) {
            // 登出
            Client4.clientHandler4.channel.close();
        } else {
            Client4.clientHandler4.send(msg);
        }
    }
}
